<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// 班牌移动端应用管理
class Wxtemplate {
    // const APPLI_NOTIFY = 'notify';  // 班级通知

    public function __construct(){
        $this->CI = &get_instance();
        $this->CI->load->model('wxUserModel');
        $this->CI->load->model('schoolModel');

        $this->CI->load->library('wechat');
    }

    // 绑定某个用户的微信号，默认为学生
    public function send_template_msg_for_user($userId, $bindUserType, $templateId, $data, $url, $includeExpired=0) {
        if(!$includeExpired && $bindUserType == BIND_TYPE_STUDENT) {
            $studentInfo = $this->CI->schoolModel->get_student_info($userId);
            // 过滤已删除
            if($studentInfo['is_delete'] == 1) {
                return;
            }
            $schoolInfo = $this->CI->schoolModel->get_school_info($studentInfo['school_id']);
            if($schoolInfo['expire_date']) {
                $schoolExpireDate = $schoolInfo['expire_date'];
                $studentExpireDate = $studentInfo['expire_date'];
                if($studentExpireDate) {
                    if($studentExpireDate > $schoolExpireDate) {
                        $expireDate = $studentExpireDate;
                    } else {
                        $expireDate = $schoolExpireDate;
                    }
                } else {
                    $expireDate = $schoolExpireDate;
                }
                if($expireDate <= date('Y-m-d')) {
                    $user['expired'] = 1;
                } else {
                    $user['expired'] = 0;
                }
            } else {
                $user['expired'] = 0;
            }
            if($user['expired'] == 1) {
                // 不发送
                return;
            }
        }
        $bindParams['isDelete'] = 0;
        $bindParams['bindUserType'] = $bindUserType;
        $bindParams['bindUserId'] = $userId;
        // 获取学生绑定列表
        $bindList = $this->CI->wxUserModel->get_bind_user_list($bindParams);
        $openIdArr = array();
        $wxUserIdArr = array();
        foreach($bindList as $bind) {
            $wxUserId = $bind['wx_user_id'];
            if(!in_array($wxUserId, $wxUserIdArr)) {
                array_push($wxUserIdArr, $wxUserId);
            }
        }
        if(empty($wxUserIdArr)) {
            return;
        }
        // 获取绑定学生的微信账号
        $wxUserParams['idArr'] = $wxUserIdArr;
        $wxUserList = $this->CI->wxUserModel->get_wx_user_list($wxUserParams);
        foreach($wxUserList as $wxUser) {
            array_push($openIdArr, $wxUser['openid']);
        }
        foreach($openIdArr as $openId) {
            $this->CI->wechat->send_template_msg($templateId, $openId, $data, $url);
        }
    }

    // 绑定某个班级学生的微信号
    public function send_template_msg($classIds, $templateId, $data, $url, $includeExpired=0) {
        // 获取班级学生
        $classIdArr = explode(',', $classIds);
        $studentIdArr = array();
        $schoolList = $this->CI->schoolModel->get_school_list(array('isDelete' => 0));
        // 使用id作为键值
        $schoolList = array_value_to_key($schoolList, 'id');
        foreach($classIdArr as $classId) {
            $params['isDelete'] = 0;
            $params['classId'] = $classId;
            // $params['field'] = 'id';
            $students = $this->CI->schoolModel->get_student_list($params);

            // 过期处理
            if(!$includeExpired) {
                foreach($students as $student) {
                    if(isset($schoolList[$student['school_id']])) {
                        $schoolInfo = $schoolList[$student['school_id']];
                        // var_export($schoolInfo); exit;
                        if($schoolInfo['expire_date']) {
                            $schoolExpireDate = $schoolInfo['expire_date'];
                            $studentExpireDate = $student['expire_date'];
                            if($studentExpireDate) {
                                if($studentExpireDate > $schoolExpireDate) {
                                    $expireDate = $studentExpireDate;
                                } else {
                                    $expireDate = $schoolExpireDate;
                                }
                            } else {
                                $expireDate = $schoolExpireDate;
                            }
                            if($expireDate <= date('Y-m-d')) {
                                $expired = 1;
                            } else {
                                $expired = 0;
                            }
                        } else {
                            $expired = 0;
                        }
                        if($expired == 0) {
                            array_push($studentIdArr, $student['id']);
                        }
                    }             
                }
            } else {
                foreach($students as $student) {
                    array_push($studentIdArr, $student['id']);
                }
            }
        }
        // var_export($studentIdArr); exit;
        $studentIdArr = array_unique($studentIdArr);
        if(empty($studentIdArr)) {
            return;
        }
        // 获取学生绑定账号
        $bindParams['isDelete'] = 0;
        $bindParams['bindUserType'] = BIND_TYPE_STUDENT;
        $bindParams['bindUserIdArr'] = $studentIdArr;
        $bindList = $this->CI->wxUserModel->get_bind_user_list($bindParams);
        $openIdArr = array();
        $wxUserIdArr = array();
        foreach($bindList as $bind) {
            $wxUserId = $bind['wx_user_id'];
            if(!in_array($wxUserId, $wxUserIdArr)) {
                array_push($wxUserIdArr, $wxUserId);
            }
        }
        if(empty($wxUserIdArr)) {
            return;
        }
        // 获取绑定学生的微信账号
        $wxUserParams['idArr'] = $wxUserIdArr;
        $wxUserList = $this->CI->wxUserModel->get_wx_user_list($wxUserParams);
        foreach($wxUserList as $wxUser) {
            array_push($openIdArr, $wxUser['openid']);
        }
        // var_export($openIdArr);exit;
        foreach($openIdArr as $openId) {
            $this->CI->wechat->send_template_msg($templateId, $openId, $data, $url);
        }
    }

    // 绑定某个学校学生和教师的微信号，只用于发校园公告，用于其他需要判断卡号过期
    public function send_template_msg_for_school($schoolId, $templateId, $data, $url) {
        $wxUserIdArr = array();
        $openIdArr = array();
        // 获取年级学生
        $studentIdArr = array();
        $params['isDelete'] = 0;
        $params['schoolId'] = $schoolId;
        $params['field'] = 'id';
        $students = $this->CI->schoolModel->get_student_list($params);
        foreach($students as $student) {
            array_push($studentIdArr, $student['id']);
        }
        if($studentIdArr) {
            // 获取学生绑定账号
            $bindParams['isDelete'] = 0;
            $bindParams['bindUserType'] = BIND_TYPE_STUDENT;
            $bindParams['bindUserIdArr'] = $studentIdArr;
            $bindList = $this->CI->wxUserModel->get_bind_user_list($bindParams);
            foreach($bindList as $bind) {
                $wxUserId = $bind['wx_user_id'];
                if(!in_array($wxUserId, $wxUserIdArr)) {
                    array_push($wxUserIdArr, $wxUserId);
                }
            }
        }
        // 获取年级教师
        $teacherIdArr = array();
        $params['isDelete'] = 0;
        $params['schoolId'] = $schoolId;
        $params['field'] = 'id';
        $teachers = $this->CI->schoolModel->get_teacher_list($params);
        foreach($teachers as $teacher) {
            array_push($teacherIdArr, $teacher['id']);
        }
        if($teacherIdArr) {
            // 获取教师绑定账号
            $bindParams['isDelete'] = 0;
            $bindParams['bindUserType'] = BIND_TYPE_TEACHER;
            $bindParams['bindUserIdArr'] = $teacherIdArr;
            $bindList = $this->CI->wxUserModel->get_bind_user_list($bindParams);
            foreach($bindList as $bind) {
                $wxUserId = $bind['wx_user_id'];
                if(!in_array($wxUserId, $wxUserIdArr)) {
                    array_push($wxUserIdArr, $wxUserId);
                }
            }
        }
        if($wxUserIdArr) {
            // 获取绑定的微信账号
            $wxUserParams['idArr'] = $wxUserIdArr;
            $wxUserList = $this->CI->wxUserModel->get_wx_user_list($wxUserParams);
            foreach($wxUserList as $wxUser) {
                array_push($openIdArr, $wxUser['openid']);
            }
            if($openIdArr) {
                $openIdArr = array_unique($openIdArr);
                foreach($openIdArr as $openId) {
                    $this->CI->wechat->send_template_msg($templateId, $openId, $data, $url);
                }
            }
        }
    }
}